﻿using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Shell;
using WPFTaskbar.Common;

namespace WPFTaskbar.Examples
{
    /// <summary>
    /// Example2.xaml 的交互逻辑
    /// </summary>
    public partial class Example2 : Window
    {
        public Example2()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
            this.TaskbarItemInfo.Overlay = null;
        }

        private void btnIconReset_Click(object sender, RoutedEventArgs e)
        {
            this.TaskbarItemInfo.Overlay = null;
        }

        private void StackPanel_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;
            if (btn != null)
            {
                System.Windows.Controls.Image img = btn.Content as System.Windows.Controls.Image;
                if (img != null)
                {
                    this.TaskbarItemInfo.Overlay = img.Source;
                }
            }

        }

        private void SlidValue_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.TaskbarItemInfo.ProgressValue = SlidValue.Value / 100;
            //or
            //TaskbarManager.Instance.SetProgressValue((int)SlidValue.Value, 100);
           
        }

        private void cmbEstilos_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem selectedItem = cmbEstilos.SelectedItem as ComboBoxItem;
           
            string selectText = selectedItem.Content.ToString();
            switch (selectText)
            {
                case "Normal":
                    {
                        Pbar.IsIndeterminate = false;
                        this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
                        //TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
                        break;
                    }
                case "Error":
                    {
                        Pbar.IsIndeterminate = false;
                        this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Error;
                        //TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Error);
                        break;
                    }
                case "Stop":
                    {
                        Pbar.IsIndeterminate = false;
                        this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Paused;
                        //TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Paused);
                        break;
                    }
                case "Indeterminate":
                    {
                        Pbar.IsIndeterminate = true;
                        this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate;
                        //TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Indeterminate);
                        break;
                    }
                case "None":
                    {
                        Pbar.IsIndeterminate = false;
                        this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
                        //TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
                        break;
                    }
                default: break;
            }
        }

        private void btnFlash_Click(object sender, RoutedEventArgs e)
        {
            FlashWindowHelper helper = new FlashWindowHelper();
            helper.Flash(8, 400, this);
        }

        TabbedThumbnail newPreview = null;
        private void btnSet_Click(object sender, RoutedEventArgs e)
        {
            newPreview = new TabbedThumbnail(this, ui, new Vector(0,0));
            newPreview.Tooltip = "Welcome to Windows 7";
            TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(newPreview);
            TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(newPreview);
           
        }
        TabbedThumbnail nextPreview = null;
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            nextPreview = new TabbedThumbnail(this, btnAdd, new Vector(0, 0));
            TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(nextPreview);
        }

        private void btnChangeOrder_Click(object sender, RoutedEventArgs e)
        {
            TabbedThumbnailManager.SetTabOrder(nextPreview,newPreview);
        }

        private void btnTool_Click(object sender, RoutedEventArgs e)
        {

            ThumbnailToolBarButton buttonFirst = new ThumbnailToolBarButton(new Icon("Images/2.ico"), "First Image");
            buttonFirst.Enabled = true;
            buttonFirst.Click += buttonFirst_Click;

            ThumbnailToolBarButton buttonPrevious = new ThumbnailToolBarButton(new Icon("Images/3.ico"), "Previous Image");
            buttonPrevious.Enabled = true;
            buttonPrevious.Click += buttonPrevious_Click;

            ThumbnailToolBarButton buttonNext = new ThumbnailToolBarButton(new Icon("Images/4.ico"), "Next Image");
            buttonPrevious.Enabled = true;
            buttonNext.Click += buttonNext_Click;

            ThumbnailToolBarButton buttonLast = new ThumbnailToolBarButton(new Icon("Images/5.ico"), "Last Image");
            buttonPrevious.Enabled = true;
            buttonLast.Click += buttonLast_Click;

            TaskbarManager.Instance.ThumbnailToolBars.AddButtons(
               new WindowInteropHelper(Application.Current.MainWindow).Handle,
                buttonFirst, buttonPrevious, buttonNext, buttonLast); 
        }

        void buttonLast_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            Console.WriteLine("buttonLast_Click");
        }

        void buttonNext_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            Console.WriteLine("buttonNext_Click");
        }

        void buttonPrevious_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            Console.WriteLine("buttonPrevious_Click");
        }

        void buttonFirst_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            Console.WriteLine("buttonFirst_Click");
        }

     

        private System.Windows.Shell.JumpList jumpList;
        private string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
        /// <summary>
        /// “User Tasks” 包含了应用程序本身提供的一些功能，通过这些链接可以直接对应用程序进行操作。例如，打开一个新IE 标签。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUserTasks_Click(object sender, RoutedEventArgs e)
        {
            //jumpList =System.Windows.Shell.JumpList.GetJumpList(App.Current);
            //jumpList.JumpItems.Clear();
            //JumpItem notepadTask = new JumpListLink(System.IO.Path.Combine(systemPath, "notepad.exe"), "Notepad")
            //{
            //    IconReference = new IconReference(System.IO.Path.Combine(systemPath, "notepad.exe"), 0)
            //};

            //JumpItem paintTask = new JumpListLink(System.IO.Path.Combine(systemPath, "mspaint.exe"), "Paint")
            //{
            //    IconReference = new IconReference(System.IO.Path.Combine(systemPath, "mspaint.exe"), 0)
            //};

            //List<string> str = new List<string>();
            //JumpItem jlSeparator = new System.Windows.Shell.JumpItem();

            //JumpItem linkTask = new JumpListLink("http://gnielee.cnblogs.com", "Gnie's Blog")
            //{
            //    IconReference = new IconReference("C:\\Program Files\\Internet Explorer\\iexplore.exe", 0)
            //};

            //jumpList.JumpItems.Add(notepadTask);
            //jumpList.JumpItems.Add( paintTask);
            //jumpList.JumpItems.Add(jlSeparator);
            //jumpList.JumpItems.Add(linkTask);
            //jumpList.JumpItems.Refresh();
        }

        /// <summary>
        /// “Known Category” 这个列表是Windows 7 默认类别，其中包含三种模式：“Recent”（近期浏览）、“Frequent”（经常浏览）、“Neither”。
        /// 下图即为Frequent 模式，它的功能是将经常浏览的网页内容记录下来以便日后再次浏览，随着时间的流逝该列表中的网页链接会随之变化或消失。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnKnownCategory_Click(object sender, RoutedEventArgs e)
        {
            //if (!Utilities.IsApplicationRegistered(TaskbarManager.Instance.ApplicationId))
            //{
            //    Utilities.RegisterFileAssociations(TaskbarManager.Instance.ApplicationId, false,
            //         TaskbarManager.Instance.ApplicationId, Assembly.GetExecutingAssembly().Location,
            //         ".jpg", ".png", ".gif", ".JPG", ".PNG", ".GIF");
            //}
            //jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;
            //CommonOpenFileDialog cfd = new CommonOpenFileDialog();
            //cfd.ShowDialog();
            //jumpList.Refresh();

            //switch (this.knownCategory.SelectionBoxItem.ToString())
            //{
            //    case "None":
            //        jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Neither;
            //        break;
            //    case "Recent":
            //        jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;
            //        break;
            //    case "Frequent":
            //        jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Frequent;
            //        break;
            //}
            //jumpList.KnownCategoryOrdinalPosition = Convert.ToInt32(this.categoryPostion.SelectionBoxItem.ToString());
            //jumpList.Refresh();
        }

        private JumpListCustomCategory customCategory;
        private void btnCustomCategory_Click(object sender, RoutedEventArgs e)
        {
            //if (this.categoryName.Text.Length > 0)
            //{
            //    customCategory = new JumpListCustomCategory(this.categoryName.Text);
            //    jumpList.AddCustomCategories(customCategory);

            //    JumpListLink jlItem = new JumpListLink(Assembly.GetExecutingAssembly().Location, this.categoryName.Text + ".png")
            //    {
            //        IconReference = new IconReference(Assembly.GetEntryAssembly().Location, 0)
            //    };
            //    customCategory.AddJumpListItems(jlItem);
            //    jumpList.Refresh();
            //}
        }
    }
}
